package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.TagMapper;
import com.itheima.pojo.Tag;
import com.itheima.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public void add(Map<String, Object> tagVO) {
        Tag tag = JSON.parseObject(JSON.toJSONString(tagVO), Tag.class);
        tagMapper.insert(tag);
    }

    @Override
    public void deleteBatch(List<Map<String, Object>> tagVoList) {
        if(tagVoList != null){
            for (Map<String, Object> tagVo : tagVoList) {
                String uid = (String) tagVo.get("uid");
                tagMapper.deleteByPrimaryKey(uid);
                }
        }
    }

    @Override
    public void edit(Map<String, Object> tagVO) {
        Tag tag = JSON.parseObject(JSON.toJSONString(tagVO), Tag.class);
        String uid = tag.getUid();
        if(uid.length() != 32){
            throw new RuntimeException("uid's length is not right");
        }
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    @Override
    public Page<Tag> getList(Map<String, Object> tagVO) {
        int pageSize = (Integer) tagVO.get("pageSize");
        int currentPage = (Integer)tagVO.get("currentPage");

        PageHelper.startPage(currentPage,pageSize);
        Page<Tag> tags = (Page<Tag>) tagMapper.selectAll();
//        for (Tag tag : tags) {
//            Date _createTime = tag.getCreateTime();
//            Date _updateTime = tag.getUpdateTime();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String createTime = sdf.format(_createTime);
//            String updateTime = sdf.format(_updateTime);
//            tag.setUpdateTime();
//        }
        return tags;
    }

    @Override
    public void stick(Map<String, Object> tagVO) {
        String uid = (String) tagVO.get("uid");
        if(uid == null){
            throw new RuntimeException("uid未传入");
        }
        Long sortMax = tagMapper.getSortMax();
        Tag tag = tagMapper.selectByPrimaryKey(uid);
        if(tag.getSort() >= sortMax){
            throw new RuntimeException("标签已置顶");
        }
        tag.setSort(sortMax + 1);
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    //根据博客引用量排序标签
    @Override
    public List<Map<String,Object>> tagSortByCite() {
        List<Map<String,Object>> tags = tagMapper.tagSortByCite();
        if(tags != null){
            for (Map<String, Object> tag : tags) {
                String uid = (String) tag.get("uid");
                Long count = (Long) tag.get("count");
                Tag tag1 = new Tag();
                tag1.setUid(uid);
                tag1.setSort(count);
                tagMapper.updateByPrimaryKeySelective(tag1);
            }
        }
        return tags;
    }

    @Override
    public List<Tag> tagSortByClickCount() {
        List<Tag> tags = tagMapper.selectAll();
        for (Tag tag : tags) {
            Long clickCount = tag.getClickCount();
            tag.setSort(clickCount);
            tagMapper.updateByPrimaryKeySelective(tag);
        }
        return tags;
    }

    @Override
    public List<Tag> getTagList() {
        return tagMapper.selectAll();
    }

    //从传入的参数map中取出数据，并填入tag对象中
    private Tag getTag(Map<String,Object> tagVO){
        if(tagVO != null){
            //取出数据
            String content = (String) tagVO.get("content");
            Long sort = Long.parseLong((String) tagVO.get("sort"));
            Integer status = Integer.parseInt((String)tagVO.get("status")) ;
            String uid = (String) tagVO.get("uid");
            //存入条件
            Tag tag = new Tag();
            tag.setUid(uid);
            tag.setContent(content);
            tag.setSort(sort);
            tag.setStatus(status);

            return tag;
        } else {
            return null;
        }
    };


}
